package com.gxyk.adapter;

import java.util.ArrayList;
import java.util.List;

import com.example.jiemian.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class CommodityAdapter extends BaseAdapter {
	private Context context;
	private List<String> datas = new ArrayList<String>();
	LayoutInflater inflater = null;
	private int firstVisibleItem = 0;//当前显示的第几个item

	private int duration = 1000;
	private Animation push_right_in;

	public CommodityAdapter(Context context, List<String> datas) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		push_right_in = AnimationUtils.loadAnimation(context, R.anim.push_right_in);
		this.datas = datas;
	}

	public void setFirstVisibleItem(int firstVisibleItem) {
		this.firstVisibleItem = firstVisibleItem;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Integer.MAX_VALUE;
		// return datas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position % datas.size());
		// return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_commodity, null);
			holder.contentRl = (RelativeLayout) convertView.findViewById(R.id.contentRl);
			holder.otherTitleRl = (RelativeLayout) convertView.findViewById(R.id.otherTitleRl);
			holder.otherTitleIv=(ImageView) convertView.findViewById(R.id.otherTitleIv);
			holder.contentIM=(ImageView) convertView.findViewById(R.id.contentIM);
			convertView.setTag(holder);
			push_right_in.setDuration(duration);
			convertView.setAnimation(push_right_in);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		position = position % datas.size();
		firstVisibleItem = firstVisibleItem % datas.size();

		// TODO 不能动的逻辑
		if (firstVisibleItem > position) {
			position = position + datas.size();
		}
		LayoutParams para = holder.otherTitleRl.getLayoutParams();
		// para.height = 1000 / 12;
		int tempValue = Math.abs(position - firstVisibleItem);
		if (tempValue < 6) {
			para.width = 1080 * 2 / 3 / 7 * (tempValue + 1);
			holder.otherTitleRl.setVisibility(View.VISIBLE);
			holder.contentRl.setVisibility(View.GONE);
			//holder.otherTitleRl.setBackgroundResource(R.drawable.shangpin_item2);
			holder.otherTitleRl.setPadding(4, 4, 0, 0);
		} else if (tempValue == 6) {
			holder.otherTitleRl.setVisibility(View.GONE);
			holder.contentRl.setVisibility(View.VISIBLE);
		} else {
			para.width = 1080 * 2 / 3 / 7 * (14 - (tempValue + 1));
			holder.otherTitleRl.setVisibility(View.VISIBLE);
			holder.contentRl.setVisibility(View.GONE);
		//	holder.otherTitleRl.setBackgroundResource(R.drawable.shangpin_item2);
			holder.otherTitleRl.setPadding(4, 0, 0, 4);
		}
		holder.otherTitleRl.setLayoutParams(para);
		return convertView;
	}

	class ViewHolder {
		RelativeLayout contentRl;
		RelativeLayout otherTitleRl;
		ImageView otherTitleIv;
		ImageView  contentIM;
	}
}
